package metricsdata;

import java.util.ArrayList;


/**
 * Metric that holds an list of Doubles.
 * 
 * 
 */
public class DoubleArrayValueMetric extends AbstractMetricAttribute {

	private ArrayList<Double> value;
	
	public DoubleArrayValueMetric(ArrayList<Double> value, String shortcut, String fullName, String description) {
		super(shortcut,fullName,description);
		
		this.value = value;
	}

	/**
	 * Constructor for String representations of value.
	 * String representation:
	 * [0.1,4.5,-1.0,0.0,null]
	 * @param value
	 * @param idOfMetricNamesInterface
	 */
	public DoubleArrayValueMetric(String value, String shortcut, String fullName, String description) {
		super(shortcut,fullName,description);
		
		if(value==null) return;
		
		this.value = new ArrayList<Double>();
		
		//check format
		if( ! value.matches("(\\[((-?\\d+\\.\\d+)|null|NaN|Infinity)(,(-?\\d+\\.\\d+)|,null|,NaN|,Infinity)*\\])|(\\[\\])")){
			System.err.println("DoubleArrayValueMetric: wrong format: '"+value+"' for "+shortcut);
			return;
		}
		
		//empty list
		if(value.equals("[]")){
			return;
		}
		
		//strip [ and ] from value
		String value2 = value.substring(1, value.length()-1);
		
		//split by ","
		String[] split = value2.split(",");
		
		//iterate over items and add them to the array list
		for(String item : split){
			if(item.equals("null")){
				this.value.add(null);
			}
			else{
				try{
					this.value.add(new Double(item));
				}
				catch(NumberFormatException e){
					System.err.println("DoubleArrayValueMetric: could not parse number given as String: "+item);
					continue;
				}
			}
		}
	}

	@Override
	public ArrayList<Double> getValue(){
		return value;
	}
	
	@Override
	/**
	 * Converts data into a String : [0.1,4.5,-1.0,0.0,null]
	 */
	public String toDatabaseString() {
		String result = "[";
		boolean addComma = false;
		for(Double item : value){
			//add no comma for the first element
			if(addComma){
				result += ","+item;
			}
			else{
				result += item;
				addComma = true;
			}
		}
		result += "]";
		return result;
	}

	@Override
	public Double getAvgValue() {
		int itemCount = 0;
		Double sum = 0.0;
		for(Double d : value){
			if(d!=null){
				sum+=d;
				itemCount ++;
			}
		}
		return sum/itemCount;
	}

	@Override
	public Double getFirstValue() {
		if(value != null && value.size()!=0){
			return value.get(0);
		}
		else{
			return null;
		}
	}

	@Override
	public Integer getItemCount() {
		if(value != null){
			return value.size();
		}
		else{
			return null;
		}
	}

	@Override
	public Double getMaxValue() {
		if(value != null){
			Double max = null;
			for(Double d : value){
				if(max == null || (d != null && d > max)){
					max = d;
				}
			}
			return max;
		}else{
			return null;
		}
	}

	@Override
	public Double getSumValue() {
		if(value != null){
			Double sum = 0.0;
			for(Double d : value){
				if(d!=null){
					sum += d;
				}
			}
			return sum;
		}else{
			return null;
		}
	}

	@Override
	public Double getMinValue() {
		if(value != null){
			Double min = null;
			for(Double d : value){
				if(min == null || (d!=null && d < min)){
					min = d;
				}
			}
			return min;
		}else{
			return null;
		}
	}

	

}
